<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/3/11
 * Time: 15:01
 */
namespace Admin\Logic;

class AdminLogic extends AdminLogicBase{
    /*
     * 登陆验证*/
    public function login(){
        $data['username']=I('post.username','','strip_tags');
        $data['password']=I('post.password','','strip_tags');
        $code=I('post.code','','strip_tags');
        $verify = new \Think\Verify();
        $res['status']=false;
        $validate_code=$verify->check($code);
        if(!$validate_code){ $res['msg']='验证码错误！';return $res; }
        if(D('Admin')->admin_count($data)==false){
            $res['msg']='用户名或密码错误！';

        } else{

            $res['status']=true;
        }

        return $res;
    }
    /*检查用户名是否重复
     * */
    public function repeat_user(){
        $key=I('post.name','','strip_tags');
        $where['username']=I('post.param','','strip_tags');
        if(I('get.id','','int')){
            $where['id']=array('neq',I('get.id','','int'));
        }
        $res['info']='无重复';
        if($key=='username' && M('Admin')->where($where)->count()==0){
            $res['status']='y';

        }else{
            $res['info']='用户名已存在！';
            $res['status']='n';

        }

        return $res;
    }
    /*检查密码*/
    public function repeat_password(){
        $key=I('post.name','','strip_tags');
        $password=I('post.param','','strip_tags');
        $res['info']='初始密码正确!';
        if($key=='password' && D('Admin')->re_psd($password)==true){
            $res['status']='y';

        }else{
            $res['info']='初始密码错误！';
            $res['status']='n';

        }
        return $res;


    }
    /*
     * 修改用户密码*/
    public function saveuser(){
        $where['username']=I('post.username','','strip_tags');
        $newpassword1=I('post.newpassword1','','strip_tags');
        $newpassword2=I('post.newpassword2','','strip_tags');
        if($newpassword2!=$newpassword1){return false;}
        if(M('Admin')->where($where)->count()>0){return false;}

        $where1['password']=I('post.password','','strip_tags');
        $field['password']=$newpassword2;
        $field['username']=$where['username'];
        return D('Admin')->saveuser($where1,$field);


    }
    /*当前控制器菜单*/
     public  function act_menu(){
        $where['app']=MODULE_NAME;
        $where['controller']=CONTROLLER_NAME;
        $where['action']=ACTION_NAME;
        $res=M('menu')->where($where)->field('id,name,parentid')->find();
        return $res;
    }

}